package com.qkbb.admin.kuibu.qkbb.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.citylist.CityList;
import com.qkbb.admin.kuibu.qkbb.citylist.putCtiycode;
import com.qkbb.admin.kuibu.qkbb.funcation.HttpURLConnHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.ImageDownloadSDCardCacheHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.OSShelp;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.url.Url;
import com.qkbb.admin.kuibu.qkbb.view.TitleBarView;
import com.qkbb.admin.kuibu.wxapi.WXEntryActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;


public class Information extends Activity {
    private String GETACCESS_TOKEN;
    private TextView citytextview;
    private TextView nicnametextview;
    private TextView sextextview;
    private TextView qianmingtextview;
    private TextView agetextview;
    private TextView heightview;
    private TextView weighttextview;
    private ImageView photoimageview;
    private String nickname;
    private String city;
    private String photo;
    private String signature;
    private String gender;
    private String age;
    private String weight;
    private String height;

    private static final String APP_ID="wx7a4daf296830c97d";
    private static final String SECRET="b864a29c3173024acd4ddde6fdf719d2";
    private IWXAPI api;
    private static String code;
    private String access_token;
    private String openid;
    private String refresh_token;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String user_token;
    private String imagename;
    private ImageDownloadSDCardCacheHelper imageDownloadSDCardCacheHelper;
    private Integer citycode;
    private putCtiycode putctiycode;
    private String strcity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        regToWx();
        sharedPreferences = getSharedPreferences("user", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
        editor=sharedPreferences.edit();
        Intent intent=getIntent();
        imagename=intent.getStringExtra("imagename");
        editor.putString("imagename", imagename);
        gender=intent.getStringExtra("gender");
        editor.putString("gender", gender);
        city=intent.getStringExtra("city");
        editor.putString("city", city);
        nickname=intent.getStringExtra("nickname");
        editor.putString("nickname", nickname);
        age=intent.getStringExtra("age");
        editor.putString("age", age);
        signature=intent.getStringExtra("signature");
        editor.putString("signature", signature);
        weight=intent.getStringExtra("weight");
        editor.putString("weight", weight);
        height=intent.getStringExtra("height");
        editor.putString("height", height);
        editor.commit();
        user_token= SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
        imageDownloadSDCardCacheHelper=new ImageDownloadSDCardCacheHelper();
        TitleBarView titleBarView= (TitleBarView) findViewById(R.id.information_titlebar);
        titleBarView.setCenterTexiView("个人资料");
        titleBarView.setColor(R.color.green);
        titleBarView.settextColor(Color.WHITE);
        titleBarView.setTextSize(15);
        citytextview= (TextView) findViewById(R.id.information_city);
        nicnametextview= (TextView) findViewById(R.id.information_textnicname);
        sextextview= (TextView) findViewById(R.id.information_textxingbie);
        qianmingtextview= (TextView) findViewById(R.id.information_textqianming);
        agetextview= (TextView) findViewById(R.id.information_textnianling);
        heightview= (TextView) findViewById(R.id.information_textshengao);
        weighttextview= (TextView) findViewById(R.id.information_texttizhong);
        photoimageview= (ImageView) findViewById(R.id.touxiangimage);
        setText();

    }


    private void regToWx(){
        api=WXAPIFactory.createWXAPI(this,APP_ID,true);
        api.registerApp(APP_ID);
    }
    public void setText(){
        nicnametextview.setText(nickname);
        if (gender.equals("1")){
            sextextview.setText("男");
        }else if (gender.equals("2")){
            sextextview.setText("女");
        }else {
            sextextview.setText("男");
        }
        qianmingtextview.setText(signature);
        agetextview.setText(age);
        heightview.setText(height);
        weighttextview.setText(weight);
        citytextview.setText(city);
        String url=new OSShelp(getApplication()).getBitmap(imagename);
        Log.e("url",url);
        imageDownloadSDCardCacheHelper.myDownloadImageSDCardCache(getApplication(), url, photoimageview, imagename, 1);
    }



    public void onClick(View v){
        switch (v.getId()){
            case R.id.information_nicheng:
                Intent intent=new Intent(Information.this, Modification.class);
                intent.putExtra("text","昵称");
                intent.putExtra("uri", Url.PUTNICKNAME);
                intent.putExtra("type","nickname");
                startActivityForResult(intent, 1);
                break;
            case R.id.information_qianming:
                Intent intent1=new Intent(Information.this, Modification.class);
                intent1.putExtra("text","个性签名");
                intent1.putExtra("uri", Url.PUTSIGNATURE);
                intent1.putExtra("type","signature");
                startActivityForResult(intent1, 2);
                break;
            case R.id.information_xingbie:
                Intent intent2=new Intent(Information.this, Modification.class);
                intent2.putExtra("text","性别");
                intent2.putExtra("uri", Url.PUTSEX);
                intent2.putExtra("type","gender");
                startActivityForResult(intent2, 3);
                break;
            case R.id.information_diqu:
                Intent intent3=new Intent(Information.this,CityList.class);
                startActivityForResult(intent3, 4);


                break;
            case R.id.information_touxiang:
                //Intent intent4=new Intent(Information.this,);
                Intent intent8=new Intent(Information.this, SelectPhoto.class);
                startActivityForResult(intent8,0);
                break;
            case R.id.information_nianling:
                Intent intent4=new Intent(Information.this, Modification.class);
                intent4.putExtra("text","年龄");
                intent4.putExtra("uri", Url.PUTAGE);
                intent4.putExtra("type","age");
                startActivityForResult(intent4,5);
                break;
            case R.id.information_shengao:
                Intent intent5=new Intent(Information.this, Modification.class);
                intent5.putExtra("text","身高");
                intent5.putExtra("uri", Url.PUTHEIGHT);
                intent5.putExtra("type", "height");
                startActivityForResult(intent5,6);
                break;
            case R.id.information_tizhong:
                Intent intent6=new Intent(Information.this, Modification.class);
                intent6.putExtra("text","体重");
                intent6.putExtra("uri", Url.PUTWEIGHT);
                intent6.putExtra("type", "weight");
                startActivityForResult(intent6,7);
                break;
            case R.id.information_weixin:
                Log.e("weixin","weixin");
                SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk_demo_test";
                Log.e("req",req.checkArgs()+"");
                api.sendReq(req);
                break;
        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("requestCode",requestCode+"");
        Log.e("resultCode",resultCode+"");
        if (data != null)
            switch (requestCode) {
                case 4:
                    city=data.getStringExtra("city");
                    citytextview.setText(city);
                    editor.putString("city", city);
                    editor.commit();
                    break;
                case 1:
                    nickname=data.getStringExtra("nickname");
                    nicnametextview.setText(nickname);
                    editor.putString("nickname", nickname);
                    editor.commit();
                    break;
                case 2:
                    signature=data.getStringExtra("signature");
                    qianmingtextview.setText(signature);
                    editor.putString("signature", signature);
                    editor.commit();
                    break;
                case 3:
                    gender=data.getStringExtra("gender");
                    if (gender.equals("1")){
                        sextextview.setText("男");
                    }else if (gender.equals("2")){
                        sextextview.setText("女");
                    }else {
                        sextextview.setText("男");
                    }
                    editor.putString("gender", gender);
                    editor.commit();
                    break;
                case 5 :
                    age=data.getStringExtra("age");
                    agetextview.setText(age);
                    editor.putString("age",age);
                    editor.commit();
                    break;
                case 6 :
                    height=data.getStringExtra("height");
                    heightview.setText(height);
                    editor.putString("height",height);
                    editor.commit();
                    break;
                case 7 :
                    weight=data.getStringExtra("weight");
                    weighttextview.setText(weight);
                    editor.putString("weight",weight);
                    editor.commit();
                    break;
                case 0:
                    imagename=data.getStringExtra("photo");
                    String url=new OSShelp(getApplication()).getBitmap(imagename);
                    Log.e("photo",imagename);
                    editor.putString("imagename",imagename);
                    editor.commit();
                    imageDownloadSDCardCacheHelper.myDownloadImageSDCardCache(this,url,photoimageview,imagename,1);

                default:
                    break;
            }
    }

    @Override
    protected void onResume() {
        super.onResume();
        SendAuth.Resp resp=WXEntryActivity.resp;
        if (resp!=null) {
             code=resp.code;
            Log.e("code",code);
            GETACCESS_TOKEN="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APP_ID+"&secret="+SECRET+"&code="+code+"&grant_type=authorization_code";
            Log.e("GETACCESS_TOKEN",GETACCESS_TOKEN);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    byte b[]=HttpURLConnHelper.loadByteFromURL(GETACCESS_TOKEN,getApplication());
                    Log.e("b",new String(b));
                    final String result=new String(b);
                    try {
                        JSONObject jsonobject=new JSONObject(result);
                        access_token=jsonobject.getString("access_token");
                        editor.putString("access_token",access_token);
                        openid=jsonobject.getString("openid");
                        editor.putString("openid",openid);
                        refresh_token=jsonobject.getString("refresh_token");
                        Log.e("access_token", access_token);
                        Log.e("openid",openid);
                        editor.putString("refresh_token", refresh_token);
                        editor.commit();
                        RequestParams params=new RequestParams();
                        params.addBodyParameter("openid",openid);
                        params.addBodyParameter("access_token", access_token);
                        params.addBodyParameter("platform", "weixin");
                        HttpUtils utils=new HttpUtils();
                        utils.send(HttpRequest.HttpMethod.POST, Url.POSTLOGIN + user_token, params, new RequestCallBack<String>() {
                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                Log.e("fshsaj", responseInfo.result);
                                try {
                                    JSONObject jsonObject=new JSONObject(responseInfo.result);
                                    JSONObject jsonObject1=jsonObject.getJSONObject("data");
                                    JSONObject jsonObject2=jsonObject1.getJSONObject("LoginResult");
                                    user_token=jsonObject2.getString("user_token");
                                    Log.e("newuser_token", user_token);
                                    SDCardHelper.saveStringToSDCardCustomDir(user_token, "user_token");
                                    String test=SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
                                    Log.e(test,test);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(HttpException e, String s) {
                                Log.e("s",s);
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("onPause","onPause");
        Intent data2=new Intent();
        Log.e("nickname",nickname);
        data2.putExtra("nickname",nickname);
        data2.putExtra("city",city);
        data2.putExtra("imagename",imagename);
        data2.putExtra("signature",signature);
        data2.putExtra("gender",gender);
        data2.putExtra("age",age);
        data2.putExtra("weight",weight);
        data2.putExtra("height",height);
        setResult(RESULT_OK,data2);
    }
}
